<include file="Public:header" />
<script type="text/javascript" src="__JSURL__/jquery.jqprint-0.3.js"></script>
<style type="text/css">
    div.select-date
    {
        margin-left:35px;
    }
    div.select-date span {
        font-weight:bold;
        position: relative;
        bottom:5px;
    }
    .confirm {
        float:right;
        margin:20px 50px 0 0;
    }
</style>
<div class="alert alert-info">
    入库清单管理
</div>
<div class="select-date">
    <span>请选择入库单的日期：</span><select name="date" onchange="changePurchase(this);">
        <volist name="allDate" id="val">
            <if condition="$_GET['date'] == $val">
                <option value="{$val}" selected>{$val}</option>
            <else />
                <option value="{$val}">{$val}</option>
            </if>
        </volist>
    </select>
</div>
<a href="" onclick="return print();" class="btn btn-primary confirm">打印入库单</a>
<a class="restore btn btn-warning confirm" onclick="return restore();" >恢复填写状态</a>
<form action="__URL__/enterAccount" method="post" onsubmit="return enterAccount(this);">
<table class="table" id="print" style="border: 1px solid #ccc;">
    <caption style="font-size:20px;margin-bottom:15px;font-weight:bold;">xx食堂 {$lastEnter.enter_date} 入库清单</caption>
    <tr>
        <th style="border: 1px solid #ccc;">物料编号</th>
        <th style="border: 1px solid #ccc;">材料品种</th>
        <th style="border: 1px solid #ccc;">材料数量</th>
        <th style="border: 1px solid #ccc;">材料单位</th>
        <th style="border: 1px solid #ccc;">材料价格</th>
        <th style="border: 1px solid #ccc;">材料总价</th>
        <th style="border: 1px solid #ccc;">入库时间</th>
    </tr>
    <if condition="$lastEnter['is_enter_stock'] eq 0">
        <volist name="stuffs" id="val">
            <tr>
                <td style="border: 1px solid #ccc;" class="number"><input type="text" name="stuff_number[]" class="input-small stuff-number" required/></td>
                <td style="border: 1px solid #ccc;" ><span class="content">{$val.1}</span> <input type="hidden" name="stuff_name[]"/></td>
                <td style="border: 1px solid #ccc;" class="num"><input class="input-small stuff-num" type="text" name="stuff_num[]" value="{$val.2}" required onkeyup="changeInput(this, 'stuff-price');"/> <input type="hidden" class="stuff-num-temp" value="{$val.2}"/></td>
                <td style="border: 1px solid #ccc;">{$val.3}</td>
                <td style="border: 1px solid #ccc;" class="price"><input class="input-small stuff-price" type="text" name="stuff_price[]" value="{$val.4}" required onkeyup="changeInput(this, 'stuff-num');"/> <input type="hidden" class="stuff-price-temp" value="{$val.4}"/></td>
                <td style="border: 1px solid #ccc;" ><span class="content stuff-count">{$val[2] * $val[4]|number_format='2'}</span> <input type="hidden" name="stuff_count[]"/></td>
                <td style="border: 1px solid #ccc;" ><span class="content">{$lastEnter.enter_date}</span> <input type="hidden" name="stuff_date[]"/></td>
                <input type="hidden" name="stuff_id[]" value="{$val.0}"/>
                <input type="hidden" name="stuff_unit[]" value="{$val.3}"/>
            </tr>
        </volist>
    <else />
        <volist name="accountData" id="val">
            <tr>
                <td style="border: 1px solid #ccc;">{$val.stuff_number}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_name}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_num}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_unit}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_price}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_count}</td>
                <td style="border: 1px solid #ccc;">{$val.stuff_date}</td>
            </tr>
        </volist>
    </if>
</table>
<input type="hidden" name="enter_id" value="{$lastEnter.id}"/>
<if condition="$lastEnter['is_enter_stock'] eq 0">
    <div class="confirm"><input type="submit" value="确定入库" class="btn btn-primary" onclick="return checkDoit('确定入库？');"/></div>
<else />
    <div class="confirm"><strong>该入库单已入库</strong></div>
</if>
</form>
<include file="Public:check"/>
<script type="text/javascript">

    function print()
    {
        var flag = true;
        $.each($('input.stuff-number'), function() {
            if($(this).val() == '')
                flag = false;
        });

        $.each($('input.stuff-num'), function() {
            if($(this).val() == '')
                flag = false;
        });

        $.each($('input.stuff-price'), function() {
            if($(this).val() == '')
                flag = false;
        });

        if(flag == false)
        {
            alert('清先填写完整空格!');
            return false;
        }
        else
        {
            $.each($('input.stuff-number'), function(index, value) {
                var val = $(this).val();
                $(this).remove();
                $('td.number').eq(index).html('<span class="c1" >'+val+'</span>');
            });

            $.each($('input.stuff-num'), function(index, value) {
                var val = $(this).val();
                $(this).remove();
                $('td.num').eq(index).html('<span class="c2" >'+val+'</span>');
            });

            $.each($('input.stuff-price'), function(index, value) {
                var val = $(this).val();
                $(this).remove();
                $('td.price').eq(index).html('<span class="c3" >'+val+'</span>');
            });

            $('#print').jqprint();
            return false;
        }
    }

    function changePurchase(_this)
    {
        var date = $(_this).val();
        location.href = '__URL__/index/date/'+date;
    }

    function restore()
    {
        $.each($('td.number').find('span'), function(index, value) {
            var val = $(this).html();
            $(this).remove();
            $('td.number').eq(index).html('<input type="text" name="stuff_number[]" class="input-small stuff-number" value="' + val + '" required />');
        });

        $.each($('td.num').find('span'), function(index, value) {
            var val = $(this).html();
            $(this).remove();
            $('td.num').eq(index).html('<input type="text" name="stuff_num[]" class="input-small stuff-num" value="' + val + '" required onkeyup="changeInput(this, \'stuff-price\');" /> <input type="hidden" class="stuff-num-temp" value="{$val.2}"/>　{$val.3}');
        });

        $.each($('td.price').find('span'), function(index, value) {
            var val = $(this).html();
            $(this).remove();
            $('td.price').eq(index).html('<input type="text" name="stuff_price[]" class="input-small stuff-price" value="' + val + '" required onkeyup="changeInput(this, \'stuff-num\');"/> <input type="hidden" class="stuff-price-temp" value="{$val.4}"/>');
        });

        return false;
    }

    function enterAccount(_this)
    {
        $.each($('span.content'), function() {
            $(this).next('input').val($(this).html());
        });
        return true;
    }

    function changeInput(_this, other)
    {
        var htmlClasses = $(_this).attr('class');
        var temp = htmlClasses.split(' ');
        var htmlClass = temp[1];
        var index = $('.'+htmlClass).index($(_this));

        if(isNaN($(_this).val()))
            $(_this).val($('.'+htmlClass+'-temp').eq(index).val());

        var count = $(_this).val() * $('.'+other).eq(index).val();
        $('span.stuff-count').eq(index).html(count.toFixed(2));
    }

    function checkDoit(msg)
    {
        if(confirm(msg))
        {
            restore();
            return true;
        }
        return false;
    }

</script>
</body>
</html>